import { Node } from "cc"
import { initData } from "../../app/GameDefine"
import { _audio, _gameType, _platform, _privacy, BaseUI, ccclass, EBlockOnceAdType, EBottomAdType, EPlatformType, EUILayer, SwitchSpriteCC } from "../../main/script/Main"


@ccclass("SettingUI")
export class SettingUI extends BaseUI {
	public layerType = EUILayer.Panel
	public blockOnceAdType = EBlockOnceAdType.Show

	private cSwitchChildrenCCMusic: SwitchSpriteCC = null!
	private cSwitchChildrenCCSound: SwitchSpriteCC = null!
	private cSwitchChildrenCCBtn: SwitchSpriteCC = null!
	private cBUser: Node = null!
	private cBDec: Node = null!

	protected onCreate(): void {
		switch (_platform.type) {
			case EPlatformType.wx:
				this.bottomAdType = EBottomAdType.Native
				break;
			default:
				this.bottomAdType = EBottomAdType.Banner
				break;
		}
	}

	protected onOpen() {
		this.updateView()
		this.cBUser.active = initData.openPrivacy
		this.cBDec.active = initData.openPrivacy
		this.cSwitchChildrenCCBtn.index = _gameType.isRun ? 0 : 1
	}


	private onClickBReset() {
		if (_gameType.hasRun()) {
			this.closeUI()
			_gameType.reset()
		}
	}

	private onClickBExit() {
		this.closeUI()
		_gameType.settingExit()
	}

	private onClickBUser() {
		_privacy.openUserUI()
	}


	private onClickBDec() {
		_privacy.openDecUI()
	}

	private onClickSwitchChildrenCCMusic() {
		if (_audio.getVolume(true) == 1)
			_audio.setVolume(true, 0)
		else
			_audio.setVolume(true, 1)
		this.updateView()
	}

	private onClickSwitchChildrenCCSound() {
		if (_audio.getVolume(false) == 1)
			_audio.setVolume(false, 0)
		else
			_audio.setVolume(false, 1)
		this.updateView()
	}

	private updateView() {
		this.cSwitchChildrenCCMusic.index = _audio.getVolume(true) == 1 ? 0 : 1
		this.cSwitchChildrenCCSound.index = _audio.getVolume(false) == 1 ? 0 : 1
	}


}
